package com.aiobc.community.common.business.service.impl;

import com.aiobc.community.common.business.entity.DeviceInfo;
import com.aiobc.community.common.business.mapper.DeviceInfoMapper;
import com.aiobc.community.common.business.service.DeviceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.aiobc.community.common.business.vo.resp.DeviceInfoVO;
import com.aiobc.community.common.business.vo.req.DeviceInfoQryReq;
import com.aiobc.community.common.business.vo.req.DeviceInfoAddReq;
import com.aiobc.community.common.business.vo.req.DeviceInfoUpdateReq;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备基本信息 服务实现类
 * </p>
 *
 * @author zhangzl
 * @since 2021-01-20
 */
@Service
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements DeviceInfoService {

    @Override
    public Page<DeviceInfoVO> pageExt(Page page, DeviceInfoQryReq req){

        Page<DeviceInfo> entityPage = page(page,Wrappers.<DeviceInfo>lambdaQuery());

        Page<DeviceInfoVO> resultPage = new Page(page.getCurrent(),page.getSize(),entityPage.getTotal());
        resultPage.setRecords(entityPage.getRecords().stream().map(e->{
            DeviceInfoVO vo = new DeviceInfoVO();
            BeanUtils.copyProperties(e,vo);
            return vo;
        }).collect(Collectors.toList()));
        return resultPage;
    }

    @Override
    public List<DeviceInfoVO> listExt(DeviceInfoQryReq req){
        List<DeviceInfo> entityList = list(Wrappers.<DeviceInfo>lambdaQuery());

        return entityList.stream().map(e->{
            DeviceInfoVO vo = new DeviceInfoVO();
            BeanUtils.copyProperties(e,vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public DeviceInfoVO getByIdExt(String id){
        DeviceInfoVO vo = new DeviceInfoVO();
        DeviceInfo entity = getById(id);
        if (null == entity){
            return vo;
        }
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public Boolean saveExt(DeviceInfoAddReq req){
        DeviceInfo entity = new DeviceInfo();
        BeanUtils.copyProperties(req,entity);

        return save(entity);
    }

    @Override
    public Boolean updateByIdExt(DeviceInfoUpdateReq req){
        DeviceInfo entity = new DeviceInfo();
        BeanUtils.copyProperties(req,entity);

        return updateById(entity);
    }

}
